package com.example.demo.controller;

import cn.hutool.core.date.DateTime;
import cn.hutool.core.date.DateUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.example.demo.common.Result;
import com.example.demo.entity.Activity;
import com.example.demo.entity.ActivitySign;
import com.example.demo.entity.AppointmentSign;
import com.example.demo.mapper.ActivityMapper;
import com.example.demo.mapper.ActivitySignMapper;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;


/**
 * @descreption:
 * @author: xhr
 * @date: 2022/09/29
 */
@RestController
@RequestMapping("/activity")
public class ActivityController {
    @Resource
    ActivityMapper activityMapper;

    @Resource
    ActivitySignMapper activitySignMapper;

    //增
    @PostMapping
    public Result<?> save(@RequestBody Activity activity) {
        activityMapper.insert(activity);
        return Result.success();
    }

    // 改
    @PutMapping
    public Result<?> update(@RequestBody Activity activity) {
        activityMapper.updateById(activity);
        return Result.success();
    }

    // 删
    @DeleteMapping("/{id}")
    public Result<?> delOne(@PathVariable Long id) {
        activitySignMapper.delete(new QueryWrapper<ActivitySign>().eq("activity_id", id));
        activityMapper.deleteById(id);
        return Result.success();
    }

    //批量删除
    @DeleteMapping("/delBatch/{ids}")
    public Result<?> delAll(@PathVariable("ids") Integer[] ids) {
        List<Integer> list = Arrays.asList(ids);
        for (int id : list) {
            activitySignMapper.delete(new QueryWrapper<ActivitySign>().eq("activity_id", id));
            System.out.println(id);
        }
        activityMapper.deleteBatchIds(list);
        return Result.success();
    }

    //全查
    @GetMapping
    public Result<?> findAll() {
        return Result.success(
                activityMapper.selectList(null)
        );
    }

    //改活动状态
    private void setState(Activity activity) {
        String time = activity.getTime();
        DateTime dateTime = DateUtil.parse(time, "yyyy-MM-dd HH:mm");
        Date now = new Date();
        if (DateUtil.compare(now, dateTime) < 0) {
            activity.setState("未开始");
        } else {
            activity.setState("已开始");
        }
    }

    //查
    @GetMapping("/page")
    public Result<?> findPage(@RequestParam(defaultValue = "10") Integer pagesize,
                              @RequestParam(defaultValue = "1") Integer pagenum,
                              @RequestParam(defaultValue = "") String search) {
        LambdaQueryWrapper<Activity> wrapper = Wrappers.<Activity>lambdaQuery();
        if (StrUtil.isNotBlank(search)) {
            wrapper.like(Activity::getName, search);
        }
        return Result.success(activityMapper.selectPage(new Page<>(pagenum, pagesize), wrapper));
    }

    //小程序显示预约和控制预约数量
    @GetMapping("/page/front")
    public Result<?> enableExam() {
        List<Activity> activityList = activityMapper.selectList(null);
        List<ActivitySign> activitySignList = activitySignMapper.selectList(null);
        for (Activity activity : activityList) {
            activity.setSurplus(activity.getLimitPeople());
            Integer activityId = activity.getId();
            activitySignList.stream().filter(sign -> sign.getActivityId().equals(activityId)).collect(Collectors.toList()).forEach(sign -> {
                if (activity.getSurplus() > 0) {
                    activity.setSurplus(activity.getSurplus() - 1);
                }
            });
            setState(activity);
        }

        return Result.success(activityList);
    }
}
